package com.zxct.wyserver.fragment;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.zxct.wyserver.R;
import com.zxct.wyserver.base.BaseFragment;
import com.zxct.wyserver.interactive.JavaScriptInterface;
import com.zxct.wyserver.util.StatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lanyu on 2018/1/30.
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.wb_mine)
    WebView wbMine;
    Unbinder unbinder;

    @Override
    protected View initView() {
        View view = View.inflate(getContext(), R.layout.fragment_mine, null);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        StatusBarUtils.setTranslucent(getActivity());
    }

    @Override
    protected void initDatas(View view) {
        super.initDatas(view);

        wbMine.getSettings().setJavaScriptEnabled(true); // 开启javascript支持
        wbMine.getSettings().setUseWideViewPort(true);//适配屏幕
        wbMine.getSettings().setDomStorageEnabled(true);//支持Html5标签
        wbMine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//优先使用缓存
        wbMine.getSettings().setSupportZoom(true);
        wbMine.getSettings().setAppCacheEnabled(true);
        wbMine.getSettings().setAllowFileAccess(true);//设置可以访问文件
        wbMine.getSettings().setDatabaseEnabled(true);
        wbMine.getSettings().setBuiltInZoomControls(true);

        // 设置js接口  第一个参数事件接口实例，第二个是实例在js中的别名，这个在js中会用到
        JavaScriptInterface JSInterface = new JavaScriptInterface(getActivity());
        wbMine.addJavascriptInterface(JSInterface, "JSInterface");

        //加载的动画
        final TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        animation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        animation.setDuration(300);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(0);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            wbMine.loadUrl("http://www.bluetopo.cn/WeChatPage/Page/bluetopo_test/Tenant/person_center");
                            wbMine.startAnimation(animation);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //屏蔽掉长按事件
        wbMine.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        //在同种分辨率的情况下,屏幕密度不一样的情况下,自动适配页面:
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int scale = dm.densityDpi;
        if (scale == 240) {
            wbMine.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (scale == 160) {
            wbMine.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else {
            wbMine.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
